<?php

/*
 * Get screen resolution:
 *
 * see CSS3 MediaQueries
 * 
 * $_SERVER['HTTP_UA_PIXELS'] is "240x320";
 * $_SERVER['HTTP_X_UP_DEVCAP_SCREENPIXELS'] is "240,320";
 * $_SERVER['HTTP_X_JPHONE_DISPLAY'] is "240*320";
 * $_SERVER['HTTP_X_OS_PREFS'] is "fw:240; fh:320; cd:18c; pl:3; pj:1; pa:1;pi:1;ps:0;"
 * $_SERVER['HTTP_USER_AGENT'] contains "240x320x16" or "16;240x320"
 * $_SERVER['HTTP_X_AVANTGO_SCREENSIZE'] ?
 */

namespace RedMVC;

/**
 * Browser class
 *
 * Original code see: PHP Browser Detection WordPress plugin
 *
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 * @author      MartyThornley
 */
class Browser{

    protected static $_lite = true;
    protected static $_browserInfo;
    protected static $_browscapFileLite = true;
    protected static $_useSession = true;

    const NAME_FIREFOX      = 'Firefox';
    const NAME_SAFARI       = 'Safari';
    const NAME_CHROME       = 'Chrome';
    const NAME_OPERA        = 'Opera';
    const NAME_OPERA_MINI   = 'Opera Mini';
    const NAME_IE           = 'IE';
    const NAME_ANDROID      = 'Android';
    const NAME_IPAD         = 'iPad';
    const NAME_IPHONE       = 'iPhone';
    const NAME_IPOD         = 'iPod';
    const NAME_BLACKBERRY   = 'BlackBerry';
    const NAME_NOKIA        = 'Nokia';
    const NAME_SAMSUNG      = 'Polaris Samsung';
    const NAME_NETFRONT     = 'Access NetFront';

    /**
     * use lite_php_browscap.ini (default)
     *
     * @param bool $bool
     */
    public static function setLite($bool = true){
        self::$_lite = $bool;
    }

    /**
     * save browserinfo in session (default)
     *
     * @param bool $bool
     */
    public static function setUseSession($bool = true){
        self::$_useSession = $bool;
        if(!$bool){
            unset(Session::getInstance()->browser);
        }
    }

    /**
     * parses browscap file
     *
     * @return array
     */
    protected static function _parseIni(){
        $agent      = $_SERVER['HTTP_USER_AGENT'];
        $yu         = array();
        $q_s        = array("#\.#", "#\*#", "#\?#");
        $q_r        = array("\.", ".*", ".?");        
        $hu         = null;
        
        if(self::$_lite){
            $browscap = LIB_PATH.DS.'RedMVC'.DS.'Browser'.DS.'lite_php_browscap.ini';
            self::$_browscapFileLite = true;
        }
        else{
            $browscap = LIB_PATH.DS.'RedMVC'.DS.'Browser'.DS.'php_browscap.ini';
            self::$_browscapFileLite = false;
        }
        $brows = parse_ini_file($browscap, true, INI_SCANNER_RAW);
        
        foreach($brows as $k => $t){
            if(fnmatch($k, $agent)){
                $yu['browser_name_pattern'] = $k;
                $pat = preg_replace($q_s, $q_r, $k);
                $yu['browser_name_regex'] = strtolower("^$pat$");
                foreach($brows as $g=>$r){
                    if(isset($t['Parent']) && $t['Parent'] == $g){
                        foreach($brows as $a => $b){
                            if($r['Parent'] == $a){
                                $yu = array_merge($yu, $b, $r, $t);
                                foreach($yu as $d=>$z){
                                    $l      = strtolower($d);
                                    $hu[$l] = $z;
                                }
                            }
                        }
                    }
                }
                break;
            }
        }
        return $hu;
    }

    /**
     * gets browser info from session or browscap file
     *
     * @return array
     */
    public static function getInfo(){
        if(self::$_useSession){            
            if(isset(Session::getInstance()->browser)){                
                self::$_browserInfo = Session::getInstance()->browser;                
            }
        }
        if(null === self::$_browserInfo || self::$_lite !== self::$_browscapFileLite){
            self::$_browserInfo = self::_parseIni();
            if(self::$_useSession){                
                Session::getInstance()->browser = self::$_browserInfo;                
            }
        }
        return self::$_browserInfo;
    }

    /**
     * returns if browser exists
     *
     * @return bool
     */
    public static function exists(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return false;
        }
        return true;
    }

    /**
     * gets browser name
     *
     * @return string|null
     */
    public static function getName(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        return $browserInfo['browser'];
    }

    /**
     * gets browser version
     *
     * @return string|null
     */
    public static function getVersion(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        return $browserInfo['version'];
    }

    /**
     * gets browser major version
     *
     * @return string|null
     */
    public static function getMajorVersion(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        return $browserInfo['majorver'];
    }

    /**
     * returns if browser is a mobile browser
     *
     * @return bool|null
     */
    public static function isMobile(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        if(isset($browserInfo['ismobiledevice']) && $browserInfo['ismobiledevice'] == 'true'){
            return true;
        }
        return false;
    }

    /**
     * returns if browser supports javascript
     *
     * @return bool|null
     */
    public static function supportsJavascript(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        if(isset($browserInfo['javascript']) && $browserInfo['javascript'] == 'true'){
            return true;
        }
        return false;
    }

    /**
     * returns if browser supports cookies
     *
     * @return bool|null
     */
    public static function supportsCookies(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        if(isset($browserInfo['cookies']) && $browserInfo['cookies'] == 'true'){
            return true;
        }
        return false;
    }

    /**
     * returns if browser supports css
     *
     * @return bool|null
     */
    public static function supportsCss(){
        $browserInfo = self::getInfo();
        if(null === $browserInfo){
            return null;
        }
        if(isset($browserInfo['cssversion'])){
            return true;
        }
        return false;
    }
}